package com.antt.cn.test2.http.http;

import android.content.Context;
import android.os.Handler;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by baichuan on 2017/4/24.
 */

public class HttpUtils {
    public static final int CACHE_SIZE = 10 * 1024 * 1024;


    private static HttpUtils mInstance;
    private static OkHttpClient.Builder httpBuilder;
    private static OkHttpClient http;

    private Handler handler;


    public static HttpUtils getInstances() {
        if (mInstance == null) {
            synchronized (HttpUtils.class) {
                if (mInstance == null)
                    mInstance = new HttpUtils();
            }
        }
        return mInstance;
    }

    public synchronized HttpUtils setCache(Context context) {
        httpBuilder.cache(new Cache(context.getExternalCacheDir().getAbsoluteFile(), CACHE_SIZE));
        http = httpBuilder.build();
        return mInstance;
    }


    private HttpUtils() {
        httpBuilder = new OkHttpClient().newBuilder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS);
        handler = new Handler();
        http = httpBuilder.build();
    }

    public void getAsyHttp(String url, HttpResultCallback callback) {

        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = http.newCall(request);

        delResult(call, callback);

    }

    /**
     * @param call
     * @param callback
     */
    private void delResult(Call call, final HttpResultCallback callback) {


        call.enqueue(new Callback() {

            String data;


            @Override
            public void onFailure(final Call call, final IOException e) {
                if (null != callback) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onError(call.request(), e);
                        }
                    });
                }
            }

            @Override
            public void onResponse(Call call, final Response response) {
                try {
                    data = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (null != callback) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            setData();
                        }
                    });
                }
            }


            public void setData() {
                try {
                    callback.onResponse(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }


}
